package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.domain.AppUser;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.UsersListView;
import com.bezdelnick.forum.web.page.template.AdminMainMenuTemplate;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 30.08.12
 * Time: 23:14
 * To change this template use File | Settings | File Templates.
 */
public class ShowUsersListPage extends AdminMainMenuTemplate {

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    public ShowUsersListPage() {
        this(new PageParameters());
    }

    public ShowUsersListPage(PageParameters parameters) {
        this.getPageParameters().mergeWith(parameters);
        if (!this.getPageParameters().get("action").isNull() && !this.getPageParameters().get("id").isNull()) {
            long userId = this.getPageParameters().get("id").toLong();
            String action = this.getPageParameters().get("action").toString();
            AppUser user = forumService.getAppUserDao().getUserById(userId);
            if (action.equals("disable")) {
                user.setEnabled(false);
                forumService.getAppUserDao().updateUser(user);
            } else if (action.equals("enable")) {
                user.setEnabled(true);
                forumService.getAppUserDao().updateUser(user);
            } else if (action.equals("remove")) {
                forumService.getAppUserDao().deleteUser(user.getId());
            }
        }

        List<AppUser> users = forumService.getAppUserDao().loadUsers();
        add(new UsersListView("list_users", users));
    }
}
